package com.welsy.stats.command
{
	import mx.rpc.events.ResultEvent;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.stats.vo.BriefUserResultsVO;
	import com.welsy.stats.business.GetIterUsersResultsDelegate;
	import com.welsy.stats.event.GetIterUsersResultsEvent;
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.controls.Alert;
	import com.welsy.stats.model.StatsModelLocator;
	import mx.collections.ArrayCollection;
	

	public class GetIterUsersResultsCommand implements ICommand, IResponder
	{
	  	
	  	private var smodel : StatsModelLocator = StatsModelLocator.getInstance();
	  	public function GetIterUsersResultsCommand()
		{	 
		}
	
		public function execute( event : CairngormEvent ): void
		{
			var giu_eve : GetIterUsersResultsEvent = GetIterUsersResultsEvent(event);
			var delegate : GetIterUsersResultsDelegate = new GetIterUsersResultsDelegate(this);
			delegate.getIterUsersResults(giu_eve.iterId, giu_eve.classroomId);
		}
	
		public function result( event : Object ) : void
		{				
			var xres : XMLList = XMLList(ResultEvent(event).result);
			
			var arNS : Namespace = new Namespace(NamespaceLocator.ADMINRESULTS_DATATYPES);
			
			var TAG_USERRESULTSCOLLECTION : QName = new QName(arNS, "GetIterUsersResultsResult");
			var TAG_BRIEFUSERRESULTSTYPE : QName = new QName(arNS, "BriefUserResultsType");
			
			var TAG_FIRSTNAME : QName = new QName(arNS, "FirstName");
			var TAG_LASTNAME : QName = new QName(arNS, "LastName");
			var TAG_ACHIEVEDCREDITS : QName = new QName(arNS, "AchievedCredits");
			var TAG_TESTSNUMBER : QName = new QName(arNS, "TestsNumber");
			var TAG_PASSEDTESTS : QName = new QName(arNS, "PassedTests");
			var TAG_USERID : QName = new QName(arNS, "UserId");
			
			var userRes : XMLList = xres.descendants(TAG_USERRESULTSCOLLECTION).children().children();
			var details : ArrayCollection = new ArrayCollection;
			
			for each(var brief : XML in userRes) {
				//BriefUserResultsType
				var burVO : BriefUserResultsVO = new BriefUserResultsVO;
				burVO.firstName = brief.child(TAG_FIRSTNAME);
				burVO.lastName = brief.child(TAG_LASTNAME);
				burVO.achievedCredits = brief.child(TAG_ACHIEVEDCREDITS);
				burVO.testsNumber = brief.child(TAG_TESTSNUMBER);
				burVO.passedTests = brief.child(TAG_PASSEDTESTS);
				burVO.userId = brief.child(TAG_USERID);
				details.addItem(burVO);
			}
			smodel.briefUserResults = details;
		}
	
		public function fault( event : Object ) : void
		{
			Alert.show("GetIterUsersResults.fault()");
		}
	}

}